﻿
Partial Class WebForm_ManageProduct_ImportProductHierarchy
    Inherits System.Web.UI.UserControl


    Protected Sub btnImportProductHierarchy_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImportProductHierarchy.PreRender
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnImportProductHierarchy)

    End Sub
    Protected Sub btnImportProductHierarchy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImportProductHierarchy.Click
        If Not FileUpload1.HasFile Then
            MyObj.ShowMessage("You don't select a file to import.")
            Return
        Else
            Dim clsRandom As New clsRandom()
            Dim newName As String = Server.MapPath("~/") & "tmp\" & clsRandom.Generate() '& Me.FileUpload1.PostedFile.FileName
            FileUpload1.PostedFile.SaveAs(newName)

            Try
                Dim sql As New MyStringBuilder
                Dim dat As New clsData

                sql.Length = 0
                sql.AppendFormat("select * into #tmp from ProductHierarchyOriginal where isDummy = 'Y' ")
                sql.AppendFormat(";truncate table ProductHierarchyOriginal ")
                sql.AppendFormat(";exec ImportProductHierarchyOriginal '{0}','{1}App_Data\ProductHierarchy.xml','6','|','\n' ", newName, Server.MapPath("~/"))

                sql.AppendFormat(";insert into ProductHierarchyOriginal(Code,Description,LevelName,IsDummy) ")
                sql.AppendFormat("select Code,Description,LevelName,IsDummy from #tmp ")
                sql.AppendFormat("where not exists( ")
                sql.AppendFormat("select * from ProductHierarchyOriginal where Code=#tmp.Code ")
                sql.AppendFormat("and Description=#tmp.Description ")
                sql.AppendFormat("and LevelName =#tmp.LevelName ")
                sql.AppendFormat(") ")
                dat.AccData(sql)

                MyObj.ShowMessage("Your file was Imported successfully.")
            Catch ex As Exception
                MyObj.ShowMessage("Import failed!")
            Finally
                System.IO.File.Delete(newName)
            End Try
        End If

    End Sub
 
End Class
